/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.PayMethodPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  PayMethodPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class PayMethodSqlProvider {
	public static final String TABLE_NAME = "pay_method";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param payMethod
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findPayMethods(final PayMethodPo payMethod) {
		return new CustomerSQL() {{
			SELECT("app_id,create_time,creator_id,icon,id,name,notify_url,partner,return_url,sign,update_time,updater_id,status ");
			FROM(TABLE_NAME);
			
			if (StringUtils.isNotBlank(payMethod.getAppId())) {
				WHERE("app_id=#{appId}");
			}
			if (null != payMethod.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(payMethod.getIcon())) {
				WHERE("icon=#{icon}");
			}
			if (StringUtils.isNotBlank(payMethod.getName())) {
				WHERE("name=#{name}");
			}
			if (StringUtils.isNotBlank(payMethod.getNotifyUrl())) {
				WHERE("notify_url=#{notifyUrl}");
			}
			if (StringUtils.isNotBlank(payMethod.getPartner())) {
				WHERE("partner=#{partner}");
			}
			if (StringUtils.isNotBlank(payMethod.getReturnUrl())) {
				WHERE("return_url=#{returnUrl}");
			}
			if (StringUtils.isNotBlank(payMethod.getSign())) {
				WHERE("sign=#{sign}");
			}
			if (null != payMethod.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (null != payMethod.getStatus()) {
				WHERE("status=#{status}");
			}
			if(StringUtils.isNotBlank(payMethod.getSortField())) {
				ORDER_BY(payMethod.getSortField() + (StringUtils.isNotBlank(payMethod.getSortMethod()) ? "desc" : payMethod.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != payMethod.getStart() && null != payMethod.getSize()) {
				LIMIT(payMethod.getStart(), payMethod.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param payMethod
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findPayMethodCount(final PayMethodPo payMethod) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (StringUtils.isNotBlank(payMethod.getAppId())) {
				WHERE("app_id=#{appId}");
			}
			if (null != payMethod.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(payMethod.getIcon())) {
				WHERE("icon=#{icon}");
			}
			if (StringUtils.isNotBlank(payMethod.getName())) {
				WHERE("name=#{name}");
			}
			if (StringUtils.isNotBlank(payMethod.getNotifyUrl())) {
				WHERE("notify_url=#{notifyUrl}");
			}
			if (StringUtils.isNotBlank(payMethod.getPartner())) {
				WHERE("partner=#{partner}");
			}
			if (StringUtils.isNotBlank(payMethod.getReturnUrl())) {
				WHERE("return_url=#{returnUrl}");
			}
			if (StringUtils.isNotBlank(payMethod.getSign())) {
				WHERE("sign=#{sign}");
			}
			if (null != payMethod.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (null != payMethod.getStatus()) {
				WHERE("status=#{status}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findPayMethodById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param payMethod
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addPayMethod(final PayMethodPo payMethod) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (StringUtils.isNotBlank(payMethod.getAppId())) {
				VALUES("app_id","#{appId}");
			}
			if (null != payMethod.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (StringUtils.isNotBlank(payMethod.getIcon())) {
				VALUES("icon","#{icon}");
			}
			if (StringUtils.isNotBlank(payMethod.getName())) {
				VALUES("name","#{name}");
			}
			if (StringUtils.isNotBlank(payMethod.getNotifyUrl())) {
				VALUES("notify_url","#{notifyUrl}");
			}
			if (StringUtils.isNotBlank(payMethod.getPartner())) {
				VALUES("partner","#{partner}");
			}
			if (StringUtils.isNotBlank(payMethod.getReturnUrl())) {
				VALUES("return_url","#{returnUrl}");
			}
			if (StringUtils.isNotBlank(payMethod.getSign())) {
				VALUES("sign","#{sign}");
			}
			if (null != payMethod.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
			if (null != payMethod.getStatus()) {
				VALUES("status","#{status}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param payMethod
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updatePayMethod(final PayMethodPo payMethod) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (StringUtils.isNotBlank(payMethod.getAppId())) {
				SET("app_id=#{appId}");
			}
			if (null != payMethod.getCreatorId()) {
				SET("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(payMethod.getIcon())) {
				SET("icon=#{icon}");
			}
			if (StringUtils.isNotBlank(payMethod.getName())) {
				SET("name=#{name}");
			}
			if (StringUtils.isNotBlank(payMethod.getNotifyUrl())) {
				SET("notify_url=#{notifyUrl}");
			}
			if (StringUtils.isNotBlank(payMethod.getPartner())) {
				SET("partner=#{partner}");
			}
			if (StringUtils.isNotBlank(payMethod.getReturnUrl())) {
				SET("return_url=#{returnUrl}");
			}
			if (StringUtils.isNotBlank(payMethod.getSign())) {
				SET("sign=#{sign}");
			}
			if (null != payMethod.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			if (null != payMethod.getStatus()) {
				SET("status=#{status}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delPayMethod(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
